package cn.edu.sjtu.se.dslab.project;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import cn.edu.sjtu.se.dslab.component.design.Button;

/**   
* @Title: Variable.java
* @Package cn.edu.sjtu.se.dslab.project
* @Description: 表示变量
* @author Thinker
* @date 2014-5-7
* @version V1.0
*/ 

@Entity
@Table(name = "variable")
public class Variable implements Serializable,Cloneable{
	private static final long serialVersionUID = 3266329354535100561L;

	private long id;
	
	private String name;
	
	private String type;
	
	private String init;
	
	private Project project;
	
	public Variable(){}
	
	public Variable(String name, String type, String init){
		this.name = name;
		this.type = type;
		this.init = init;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public long getId() {
		return id;
	}

	
	public void setId(long id) {
		this.id = id;
	}
	
	@Column(name = "name", nullable = true, length = 255)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Column(name = "type", nullable = true, length = 30)
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
	@Column(name = "init", nullable = true, length = 255)
	public String getInit() {
		return init;
	}
	public void setInit(String init) {
		this.init = init;
	}
	
	@ManyToOne(cascade = CascadeType.REFRESH)
	@JoinColumn(name = "project_id", nullable = true)
	public Project getProject() {
		return project;
	}
	public void setProject(Project project) {
		this.project = project;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Variable other = (Variable) obj;
		if (id != other.id)
			return false;
		return true;
	}
	
	@Override
	public Object clone() {
		Variable variable = null;
		try{
			variable = (Variable)super.clone();
			variable.setId(0);
			variable.setProject(null);
		}catch(CloneNotSupportedException ex){
			ex.printStackTrace();
		}
		return variable;
	}
	
}
